package com.wanyang.dto;

import com.wanyang.dos.JobDO;
import com.wanyang.dos.JobDOExample;
import com.wanyang.mapper.JobDOMapper;
import com.wanyang.utils.DateUtil;
import com.wanyang.utils.SpringContextUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Arrays;

@Data
public class PostPublishJob extends PostPublishBase {
    private Job job;

    @Data
    public static class Job {
        @ApiModelProperty(value = "1-求职，2-招聘")
        private Integer type;
        @ApiModelProperty(value = "求职内容")
        private String jobSearchDetail;
        @ApiModelProperty(value = "求职 - 联系人")
        private String jobSearchContact;
        @ApiModelProperty(value = "求职 - 联系电话")
        private String jobSearchMobile;
        @ApiModelProperty(value = "求职 - 岗位")
        private String jobSearchDirection;

        @ApiModelProperty(value = "招聘 - 岗位要求")
        private String recruitDemand;
        @ApiModelProperty(value = "招聘 - 工作要求")
        private String recruitWorkDetail;
        @ApiModelProperty(value = "招聘 - 公司简介")
        private String recruitCompanyIntro;
        @ApiModelProperty(value = "招聘 - 联系人")
        private String recruitContact;
        @ApiModelProperty(value = "招聘 - 联系电话")
        private String recruitMobile;
        @ApiModelProperty(value = "招聘 - 岗位")
        private String recruitDirection;
        @ApiModelProperty(value = "招聘 - 公司位置 - 经度")
        private String recruitLongitude;
        @ApiModelProperty(value = "招聘 - 公司位置 - 纬度")
        private String recruitLatitude;
    }

    @Override
    public Object getDetailObject() {
        return job;
    }

    @Override
    public AuditObject getNeedAuditObject() {
        AuditObject auditObject = new AuditObject();
        auditObject.setContents(job.type == 1 ?
                Arrays.asList(job.jobSearchDetail, job.jobSearchDirection) :
                Arrays.asList(job.recruitDemand, job.recruitWorkDetail, job.recruitCompanyIntro, job.recruitDirection) );

        return auditObject;
    }

    @Override
    public void afterPublish() {
        JobDOMapper jobDOMapper = SpringContextUtil.getBean(JobDOMapper.class);

        String jobName = this.job.type == 1 ? this.job.jobSearchDirection : this.job.recruitDirection;

        JobDOExample example = new JobDOExample();
        example.createCriteria()
                .andIsDeleteEqualTo(false)
                .andJobNameEqualTo(jobName);

        if (jobDOMapper.countByExample(example) == 0) {
            JobDO jobDO = new JobDO();
            jobDO.setJobName(jobName);
            jobDO.setCreateTime(DateUtil.currentUnixTime());
            jobDO.setIsDelete(false);
            jobDO.setAutoCreate(true);
            jobDOMapper.insertSelective(jobDO);

        }

    }
}
